package com.cyys.modules.wx.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.annotation.SystemLog;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.JSONData;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.core.sysParam.ParamConstants;
import com.cyys.modules.wx.model.user.WxUserinfo;
import com.cyys.modules.wx.service.user.WxUserinfoService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/wx/user")
public class WxUserinfoAction extends BaseAction {

	@Resource
	private WxUserinfoService wxUserinfoService;

	@GetMapping("/list")
	@RequiresPermissions("wx:user:list")
	public String list(Model model) {
		model.addAttribute("DISABLE_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.DISABLE));
		model.addAttribute("SEX_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.SEX));
		return "module/wx/user/wxUserinfo-list";
	}

	@GetMapping("/add/input")
	@RequiresPermissions("wx:user:add")
	public String addInput(Model model) throws Exception {
		model.addAttribute("DISABLE_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.DISABLE));
		model.addAttribute("SEX_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.SEX));
		return "module/wx/user/wxUserinfo-input";
	}
	
	@GetMapping("/edit/input")
	@RequiresPermissions("wx:user:edit")
	public String updateInput(WxUserinfo wxUserinfo, Model model) throws Exception {
		model.addAttribute("DISABLE_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.DISABLE));
		model.addAttribute("SEX_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.SEX));
		if (wxUserinfo != null && wxUserinfo.getId() != null) {
			model.addAttribute("wxUserinfo", wxUserinfoService.getById(wxUserinfo.getId()));
		}
		return "module/wx/user/wxUserinfo-input";
	}
	
	@ResponseBody
	@PostMapping("/add/save")
	@RequiresPermissions(value = {"wx:user:add", "wx:user:edit"},logical=Logical.OR)
	@SystemLog(module = "微信用户", methods = "微信用户管理-保存微信用户")
	public AR save(WxUserinfo wxUserinfo) throws Exception {
		return wxUserinfoService.save(wxUserinfo);
	}


	@ResponseBody
	@PostMapping("/list/listInfo")
	@RequiresPermissions("wx:user:list")
	public String listInfo(WxUserinfo wxUserinfo, Page page) throws Exception {
		return JSONData.toLayerData(wxUserinfoService.list(page , new QueryWrapper(wxUserinfo),true));
	}

	@ResponseBody
	@PostMapping("/del/deleteById")
	@RequiresPermissions("wx:user:del")
	@SystemLog(module = "微信用户", methods = "微信用户管理-删除微信用户")
	public AR deleteById(String id) throws Exception{
		return wxUserinfoService.deleteById(id);
	}

	@ResponseBody
	@PostMapping("/del/deleteByIds")
	@RequiresPermissions("wx:user:del")
	@SystemLog(module = "微信用户", methods = "微信用户管理-批量删除微信用户")
	public AR deleteByIds(String ids) throws Exception {
		return wxUserinfoService.deleteBatchIds(ids);
	}

	@GetMapping("wxUserinfoSelect")
	public String wxUserinfoSelect(Model model) {
		model.addAttribute("DISABLE_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.DISABLE));
		model.addAttribute("SEX_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.SEX));
		return "module/wx/user/wxUserinfo-select";
	}
}
